﻿#region << 版 本 注 释 >>
/*----------------------------------------------------------------
// Copyright (C) 2021 kingerrobot
// 版权所有。 
//
// 文件名：BWcsAsrsNetInput
// 文件功能描述：
//
// 创建者：名字 (zzl)
// 时间：2021/12/7 16:43:41
//
// 修改人：
// 时间：
// 修改说明：
// 版本：V1.0.0
//----------------------------------------------------------------*/
#endregion
using K8.MDCBB;
using KingerRobot.MexicoSailunMDC.Dao;
using System.Security.Cryptography.Pkcs;

namespace KingerRobot.MexicoSailunMDC.SpiService;

internal class BWcsAsrsCrnAbnormalInput : IWcsAsrsCrnAbnormalInput
{
    private readonly static IRcsLocation _RcsLocation = new BRcsLocation();
    private readonly static ITask _Task = new BTask();
    private readonly static IProdProduct _ProdProduct = new BProdProduct();


    public POWcsAsrsCrnAbnormalInput GetAsrsCrnAbnormalInPutByCrnId(long crnId)
    {
        var db = Db.Default;

        var wcsAsrsCrnInput = db.WcsAsrsCrnAbnormalInputs.Where(t => t.CrnId == crnId).FirstOrDefault();
        return wcsAsrsCrnInput;
    }


    public List<HasSkuLoc> GetHasSkuLoc(List<long?> crnIds)
    {
        // 根据堆垛机获取入库异常站台位 LSQ
        var inputs = GetWcsAsrsCrnAbnormalInputs(crnIds);
        if (inputs is null || !inputs.Any())
        {
            return new List<HasSkuLoc>();
        }

        // 一号异常放胎位；二号异常放胎位
        var loc1 = inputs.Select(t => t.InputLoc1).ToList();
        var loc2 = inputs.Select(t => t.InputLoc2).ToList();
        var ids = loc1.Union(loc2).ToList();

        // 可用放胎位
        var locations = _RcsLocation.GetLocationsByLocIds(ids);
        var locHasSkus = new List<HasSkuLoc>();

        // 所有输送任务(筛选一下Location绑定的任务Id是否包含在内)
        // var taskIds = _Task.GetTaskByType((int)EnumTaskType.Transportation).Select(t => t.Id).ToList();


        // 获取任务物料
        // var taskMaterials = _Task.GetTaskMaterial(taskIds);
        var productCodes = locations.Select(t => t.ProductCode).ToList();
        var products = _ProdProduct.GetProductBindingInfoByCodes(productCodes);

        inputs.ForEach(item =>
        {
            // 是否绑定胎胚
            var location1 = locations.Where(t => t.Id == item.InputLoc1 && t.ProductCode is not null && t.LocHasSku == 1 && t.MaterialId is not null).FirstOrDefault();
            var location2 = locations.Where(t => t.Id == item.InputLoc2 && t.ProductCode is not null && t.LocHasSku == 1 && t.MaterialId is not null).FirstOrDefault();


            var hasSkuLoc = new HasSkuLoc();
            var locBindInfos = new List<LocBindInfo>();


            if (location1 != null)
            {
                var product = products.Where(t => t.POProdProduct.ProductCode == location1.ProductCode).FirstOrDefault();
                if (product is not null)
                {
                    hasSkuLoc.MinTime = location1.TaskOccupyTime ?? location1.LocHasSkuTime;
                    locBindInfos.Add(new()
                    {
                        LocId = item.InputLoc1,
                        TaskId = location1.TaskId,
                        MaterialId = location1.MaterialId,
                        LocTime = location1.TaskOccupyTime,
                        HasGood = true,
                        InType = 1,
                        SkuId = product.POProdSku.Id.GetValueOrDefault(),
                        Index = 1,
                        Direction = item.Direction,
                    });
                }
            }

            if (location2 != null)
            {
                var product = products.Where(t => t.POProdProduct.ProductCode == location2.ProductCode).FirstOrDefault();
                if (product is not null)
                {
                    var loc2Time = location2.TaskOccupyTime ?? location2.LocHasSkuTime;
                    //OLDTODO 和1物料不同，判断是否有和gongid
                    hasSkuLoc.MinTime = hasSkuLoc.MinTime is not null && hasSkuLoc.MinTime.HasValue ?
                    hasSkuLoc.MinTime.Value > loc2Time ?
                    loc2Time : hasSkuLoc.MinTime.Value
                    : loc2Time;

                    locBindInfos.Add(new()
                    {
                        LocId = item.InputLoc2,
                        TaskId = location2.TaskId,
                        MaterialId = location2.MaterialId,
                        LocTime = location2.TaskOccupyTime,
                        HasGood = true,
                        InType = 1,
                        SkuId = product.POProdSku.Id.GetValueOrDefault(),
                        Index = 2,
                        Direction = item.Direction,
                    });
                }
            }

            if (locBindInfos.Count > 0)
            {
                hasSkuLoc.LocBindInfos = locBindInfos;
                hasSkuLoc.CrnId = item.CrnId;
                hasSkuLoc.AreaId = item.AreaId;
                locHasSkus.Add(hasSkuLoc);
            }
        });

        return locHasSkus;
    }


    private List<POWcsAsrsCrnAbnormalInput> GetWcsAsrsCrnAbnormalInputs(List<long?> crnIds)
    {
        var db = Db.Default;
        var result = db.WcsAsrsCrnAbnormalInputs.Where(t => crnIds.Contains(t.CrnId)).ToList();
        return result;
    }



}


